/**
 * 莫队算法模板题。问区间内有多少个三元组(i,j,k)满足 i < j < k 且 Ai == Aj == Ak
 * 记录一个全局答案，每次添加的元素值记作x,其加之前的数量记作c，则此次操作添加的三元组数量就是 C(c, 2)
 * 对于删除操作，令删除之后的数量是c，则此次减少的数量也是 C(C, 2)
 */
#include <bits/stdc++.h>
using namespace std;

#include <bits/extc++.h>
using namespace __gnu_pbds;

using llt = long long;

struct mo_t{ // 莫队算法无修改, 依次调用input(),run(),output()

struct query_t{
    int s;
    int e;
    int idx;
    bool operator < (const query_t & b) const {
        int ln = s / block_size();
        int rn = b.s / block_size();
        if(ln != rn) return ln < rn;
        if(e != b.e) return e < b.e;
        return idx < b.idx;
    }
};

using value_t = int;
using answer_t = llt;

vector<value_t> data;      // 原始数组
vector<query_t> questions; // 问题
vector<answer_t> ans;      // 答案

int curLeft, curRight;

llt moans;
vector<int> Cnt;

static int block_size() {
    static int const BLOCK_SIZE = 450;
    return BLOCK_SIZE;
}

void init(){
    ans.assign(questions.size(), 0);
    sort(questions.begin(), questions.end());

    curLeft = 1; curRight = 0;

    moans = 0;
    Cnt.assign(2E5+2, 0);
}

void insert(int idx){
    auto c = ++Cnt[data[idx]];
    if(c > 2) moans += (c - 1LL) * (c - 2LL) / 2LL;
}

void remove(int idx){
    auto c = --Cnt[data[idx]];
    if(c) moans -= (c - 1LL) * c / 2LL;
}

answer_t answer(){
    return moans;
}

void input(){
    int n, q; cin >> n >> q;
    data.assign(n + 1, 0);
    for(int i=1;i<=n;++i) cin >> data[i];
    questions.assign(q, {});
    int k = 0;
    for(auto & q : questions){
        cin >> q.s >> q.e;
        q.idx = k++;
    }
}

void run(){
    init();

    for(const auto & q : questions){
        while(curRight < q.e) insert(++curRight);
        while(curLeft > q.s) insert(--curLeft);
        while(curRight > q.e) remove(curRight--);
        while(curLeft < q.s) remove(curLeft++);
        ans[q.idx] = answer();
    }
}

void output(){
    for(auto i : ans) cout << i << "\n";
    // cout << endl;
}

};

mo_t Mo;

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0);
	Mo.input();
    Mo.run();
    Mo.output();
    return 0;
}

